''' <summary>
''' This is the main type for your game
''' </summary>
'''
Public Class Game1
    Inherits Microsoft.Xna.Framework.Game
    ' Elements that render to the screen / render target
    Private WithEvents graphics As GraphicsDeviceManager
    Public WithEvents spriteBatch As SpriteBatch

    ' Camera Items
    Public Camera As GameCamera
    Private Background As Background
    Private WithEvents GameRun As GameRun
    Private mLoading As Texture2D
    Private WithEvents TitleScreen As TitleScreen
    Private WithEvents LevelPicker As LevelPicker
    Private WithEvents HighScore As HighScore
    Private mblnLoading As Boolean = True
    Private mintStep As Integer = 0
    Private mlevel As Byte = 1
    ' ////////////////////////////////////////////////////////////////
    ' New Game object
    Public Sub New()
        graphics = New GraphicsDeviceManager(Me)
        graphics.IsFullScreen = True
        graphics.PreferredBackBufferWidth = 800
        graphics.PreferredBackBufferHeight = 480
        Content.RootDirectory = "Content"

        'Frame rate is 30 fps by default for Windows Phone.
        TargetElapsedTime = TimeSpan.FromTicks(333333)

        'Extend battery life under lock.
        InactiveSleepTime = TimeSpan.FromSeconds(1)
    End Sub

    ' ////////////////////////////////////////////////////////////////
    ' Initialise the components.
    Protected Overrides Sub Initialize()

        mLoading = Content.Load(Of Texture2D)("loading")
        mblnLoading = True
        MyBase.Initialize()
    End Sub
    ' ////////////////////////////////////////////////////////////////
    ' Load the data from the store
    Protected Overrides Sub LoadContent()
        ' Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = New SpriteBatch(GraphicsDevice)
        GraphicsDevice.DepthStencilState = DepthStencilState.Default
    End Sub
    ' ////////////////////////////////////////////////////////////////
    ' Clean up
    Protected Overrides Sub UnloadContent()
        Do While (Components.Count > 0)
            Components.Remove(Components(0))
        Loop
        Camera = Nothing
        Background = Nothing
        GameRun = Nothing
        TitleScreen = Nothing
    End Sub

    ' ////////////////////////////////////////////////////////////////
    ' Move the elements / camera etc, make the changes to the overlay.
    Protected Overrides Sub Update(ByVal gameTime As GameTime)
        If (mblnLoading) Then
            mintStep += 1
            Select mintStep
                Case 1
                    Background = New Background(Me)
                    Background.DrawOrder = 1
                Case 2
                    TitleScreen = New TitleScreen(Me)
                    TitleScreen.DrawOrder = 10
                Case 3
                    Camera = New GameCamera(Me)
                Case 4
                    Components.Add(Background)
                    Components.Add(TitleScreen)
                    Components.Add(Camera)
                    mblnLoading = False
                    'Case 101
                    '    Components.Remove(TitleScreen)
                    '    TitleScreen.Dispose()
                    '    TitleScreen = Nothing
                    'Case 102
                    '    GameRun = New GameRun(Me)
                    '    GameRun.DrawOrder = 11
                    '    Components.Add(GameRun)
                    '    mblnLoading = False
                Case 1001
                    Components.Remove(GameRun)
                    GameRun.Dispose()
                    GameRun = Nothing
                Case 1002
                    TitleScreen = New TitleScreen(Me)
                    TitleScreen.DrawOrder = 10
                    Components.Add(TitleScreen)
                    mblnLoading = False
                Case 501
                    Components.Remove(TitleScreen)
                    TitleScreen.Dispose()
                    TitleScreen = Nothing
                Case 502
                    LevelPicker = New LevelPicker(Me)
                    LevelPicker.DrawOrder = 11
                    Components.Add(LevelPicker)
                    mblnLoading = False
                Case 601
                    Components.Remove(LevelPicker)
                    LevelPicker.Dispose()
                    LevelPicker = Nothing
                Case 602
                    GameRun = New GameRun(Me, mlevel)
                    GameRun.DrawOrder = 11
                    Components.Add(GameRun)
                    mblnLoading = False
                Case 701
                    Components.Remove(TitleScreen)
                    TitleScreen.Dispose()
                    TitleScreen = Nothing
                Case 702
                    HighScore = New HighScore(Me)
                    HighScore.DrawOrder = 11
                    Components.Add(HighScore)
                    mblnLoading = False
            End Select
        End If
        ' Allows the game to exit
        If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
            Me.Exit()
        End If

        MyBase.Update(gameTime)
    End Sub
    ' ////////////////////////////////////////////////////////////////
    ' The game is being drawn :)
    Protected Overrides Sub Draw(ByVal gameTime As GameTime)
        ' Draw a loading screen on top if need be.
        Background.Enabled = Not mblnLoading
        If (mblnLoading) Then
            Dim State As RenderStates = New RenderStates
            RecordRenderState(State, GraphicsDevice)
            spriteBatch.Begin()
            spriteBatch.Draw(mLoading, New Vector2(0, 0), Color.White)
            spriteBatch.End()
            RestoreRenderState(State, GraphicsDevice)
        End If
        ' TODO: Add your drawing code here
        MyBase.Draw(gameTime)

    End Sub
    ' ////////////////////////////////////////////////////////////////
    ' The Start button has been pressed... so we're going to switch between the start screen and the end screen.
    Private Sub TitleScreen_Start() Handles TitleScreen.Start
        mblnLoading = True
        mintStep = 500
    End Sub

    Private Sub GameRun_ReloadTitle() Handles GameRun.ReloadTitle
        mblnLoading = True
        mintStep = 1000
    End Sub

    Private Sub LevelPicker_LevelSelected(Level As Byte) Handles LevelPicker.LevelSelected
        mblnLoading = True
        mlevel = Level
        mintStep = 600
    End Sub
End Class
